#include "my_calculator.h"

my_calculator::my_calculator(QWidget *parent) : QWidget(parent)
{
    init_calculator();
}

void my_calculator::init_calculator()
{
    //使用qtoolbutton
    qtl = new QToolButton(this);
    qtl->setIcon(QIcon(":/1.jpg"));
    qtl->setText("我的计算器");
    qtl->setIconSize(QSize(100, 100));
    // 设置图标和文本的布局方式
    qtl->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);


    //  // 使用 QLabel 作为显示屏
    display = new QLineEdit("0", this);
    display->setMinimumHeight(40);
    display->setAlignment(Qt::AlignRight | Qt::AlignVCenter); // 右对齐
    display->setStyleSheet("QLabel { background-color: lightgray; font-size: 18px; }");

    main_layout = new QVBoxLayout(this);
    main_layout->addWidget(qtl);
    main_layout->addWidget(display);


    // 创建网格布局
    gridLayout = new QGridLayout;
    gridLayout->setSpacing(5);

    // 按钮的文本标签
    QString buttonLabels[20] = {
        "7", "8", "9", "/",
        "4", "5", "6", "*",
        "1", "2", "3", "-",
        "0", ".", "=", "+",
        "C", "(", ")", "%"
    };

    int index = 0;
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 4; ++j) {
            btn[index] = new QPushButton(buttonLabels[index], this);
            btn[index]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            gridLayout->addWidget(btn[index], i, j);
            index++;
        }
    }
//    connect(button0,&QPushButton::clicked,this,[=](){
//        display->insert(static_cast<QPushButton*>(sender())->text());
//        //split  分割字符串,运用到真正的计算中
//    });
    // 将网格布局添加到主布局
    main_layout->addLayout(gridLayout);
}
